package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.QueryCgBom;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomMapper;
import com.jy.adscm.operation.mscyb.bean.JqylSettingMaterialCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylSettingMaterialCheckMapper;
import com.jy.adscm.operation.mscyb.service.JqylSettingMaterialCheckService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-06
 */
@Service
public class JqylSettingMaterialCheckServiceImpl extends ServiceImpl<JqylSettingMaterialCheckMapper, JqylSettingMaterialCheck> implements JqylSettingMaterialCheckService {
@Autowired
    JyUserMapper jyUserMapper;
@Autowired
    CgBomMapper cgBomMapper;
@Autowired
    JyDrawingTypeMapper  jyDrawingTypeMapper;
    @Override
    public List<JqylSettingMaterialCheck> selectByPid(String pid) {
        Wrapper<JqylSettingMaterialCheck> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",pid);
        wrapper.orderBy("rank",false);
        List<JqylSettingMaterialCheck> jqylSettingMaterialChecks = super.selectList(wrapper);
        for (JqylSettingMaterialCheck jqylSettingMaterialCheck : jqylSettingMaterialChecks) {
            if (AdscmBaseUtils.getInstance().isNotEmpty( jqylSettingMaterialCheck.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylSettingMaterialCheck.getCreator());
                jqylSettingMaterialCheck.setCreatorName(jyUser.getName());
            }

        }
        return jqylSettingMaterialChecks;
    }

    @Override
    public Page<CgBom> selectBom(Page<CgBom> page, QueryCgBom queryCgBom) {
        Wrapper<CgBom> wrapper=new EntityWrapper();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getBomName(),null)){
            wrapper.eq("name",queryCgBom.getBomName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgBom.getMachineType(),null)){
            wrapper.eq("machine_type",queryCgBom.getMachineType());
        }
        wrapper.eq("enable",1);
        RowBounds rowBounds=new RowBounds(queryCgBom.getCurrentPage()-1,queryCgBom.getPageSize());
        List<CgBom> CgBoms = cgBomMapper.selectPage( rowBounds, wrapper);
        for (CgBom cgBom : CgBoms) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMachineType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(cgBom.getMachineType());
                cgBom.setMachineTypeName(jyDrawingType.getType());
            }
        }
        page.setRecords(CgBoms);
        page.setTotal(cgBomMapper.selectCount(wrapper));
        return page;
    }
}
